﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using SweeperPrototype.GUI.Behaviors.AttachedCommand;
using SweeperPrototype.GUI.Positions;
using SweeperPrototype.Objects;

namespace SweeperPrototype.GUI.ExposureMappings
{
    public class ExposureMapListViewModel : BaseVM
    {
        private readonly ObservableCollection<ExposureMappingViewModel> m_ExposureMappings;
        private readonly INotifyList<ExposureMappingSubscription> m_CoreMappings;
        private readonly NotifyList<ExposureMappingSubscription> m_SelectedMappings;

        public ExposureMapListViewModel(NotifyList<ExposureMappingSubscription> core_mappings)
        {
            m_CoreMappings = core_mappings;
            m_SelectedMappings = new NotifyList<ExposureMappingSubscription>();
            m_ExposureMappings = new ObservableCollection<ExposureMappingViewModel>();

            m_CoreMappings.Addition += HandleCoreMappingAdded;
            m_CoreMappings.Removal += HandleCoreMappingRemoved;
        }

        public ObservableCollection<ExposureMappingViewModel> ExposureMappings
        {
            get { return m_ExposureMappings; }
        }

        public INotifyList<ExposureMappingSubscription> SelectedMappings { get; set; }

        private void HandleCoreMappingRemoved(ExposureMappingSubscription removal, int index)
        {
            m_ExposureMappings.RemoveAt( index );
        }

        private void HandleCoreMappingAdded(ExposureMappingSubscription addition, int index)
        {
            ExposureMappingViewModel mapping_view_model = new ExposureMappingViewModel(addition);
            m_ExposureMappings.Add( mapping_view_model );
            mapping_view_model.PropertyChanged += HandleMappingSelected;
        }

        private void HandleMappingSelected(object sender, PropertyChangedEventArgs args)
        {
            
            if( args == ExposureMappingViewModel.ARGS_ISSELECTED )
            {
                ExposureMappingViewModel view_model = (ExposureMappingViewModel)sender;
                if( view_model.IsSelected )
                {
                    m_SelectedMappings.Add(view_model.CoreExposureMapping);
                }
                else
                {
                    m_SelectedMappings.Remove(view_model.CoreExposureMapping);
                }
            }
        }

    }
}
